﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using SES.CMS.BL;
using System.Web.UI.WebControls;
using SES.CMS.DO;

namespace SES.CMS.AdminCP
{
    public partial class ListOrders : System.Web.UI.Page
    {
        cmsOrdersDO objOrder = new cmsOrdersDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowData();
            }

        }
        //ContactBLL contactData = new ContactBLL();
        cmsOrdersBL ItemBL = new cmsOrdersBL();
        private void ShowData()
        {
            if (dropSort.SelectedValue == "1")
            {
                
                if (dropIsView.SelectedValue == "0")
                {
                    gridView.DataSource = ItemBL.SelectAll();
                    gridView.DataBind();
                }
                else if (dropIsView.SelectedValue == "1")
                {
                    gridView.DataSource = ItemBL.GetByStatus(false);
                    gridView.DataBind();
                }
                else
                {
                    gridView.DataSource = ItemBL.GetByStatus(true);
                    gridView.DataBind();
                }
            }
            else if (dropSort.SelectedValue == "2")
            {
                if (dropIsView.SelectedValue == "0")
                {
                    gridView.DataSource = ItemBL.SelectAll2();
                    gridView.DataBind();
                }
                else if (dropIsView.SelectedValue == "1")
                {
                    gridView.DataSource = ItemBL.SelectAllOld(false);
                    gridView.DataBind();
                }
                else
                {
                    gridView.DataSource = ItemBL.SelectAllOld(true);
                    gridView.DataBind();
                }
            }
            

        }

        public void ResetForm()
        {
            ltAddress.Text = "";
            
            //ltContent.Text = "";
            //ltDateSend.Text = "";
            ltEmail.Text = "";
            ltName.Text = "";
            ltPhone.Text = "";
            //l/tTitle.Text = "";
        }
        protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (dropSort.SelectedValue == "1")
            //{
            //    gridView.DataSource = ItemBL.SelectAll();
            //    gridView.DataBind();
            //}
            //else if (dropSort.SelectedValue == "2")
            //{
            //    gridView.DataSource = ItemBL.SelectAllOld();
            //    gridView.DataBind();
            //}
            //lbThongBao.Text = "";
            //ShowData();
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            lbThongBao.Text = "";
            try
            {
                lbThongBao.Text = "";
                gridView.PageIndex = e.NewPageIndex;
                ShowData();
            }
            catch { }
        }
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                lbThongBao.Text = "";
                int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);
                cmsOrdersDO itemDO = new cmsOrdersDO();
                itemDO.Id = id;
                ItemBL.Delete(itemDO);
                ResetForm();
                ShowData();
            }
            catch (Exception ex)
            {
                Myfunction.ThongBao(lbThongBao, ex.Message);
            }
        }
        protected void btnXoaChon_Click(object sender, EventArgs e)
        {
            try
            {
                lbThongBao.Text = "";
                foreach (GridViewRow item in gridView.Rows)
                {
                    foreach (Control ctrl in item.Cells[1].Controls)
                    {
                        if (ctrl.GetType() == typeof(CheckBox))
                        {
                            CheckBox check = (CheckBox)ctrl;
                            if (check.Checked)
                            {
                                int id = Convert.ToInt32(item.Cells[2].Text);
                                cmsOrdersDO itemDO = new cmsOrdersDO();
                                itemDO.Id = id;
                                ItemBL.Delete(itemDO);
                            }
                            break;
                        }
                    }
                }
                ResetForm();
                ShowData();
            }
            catch (Exception ex)
            {
                Myfunction.ThongBao(lbThongBao, ex.Message);
            }
        }
        protected void linkCheckAll_Click(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            foreach (GridViewRow item in gridView.Rows)
            {
                foreach (Control ctrl in item.Cells[1].Controls)
                {
                    if (ctrl.GetType() == typeof(CheckBox))
                    {
                        CheckBox check = (CheckBox)ctrl;
                        check.Checked = true;
                        break;
                    }
                }
            }
        }
        protected void btnXuLy_Click(object sender, EventArgs e)
        {

            objOrder.Id = int.Parse(Session["Id"].ToString());
            objOrder = new cmsOrdersBL().Select(objOrder);
            objOrder.Status = true;
            new cmsOrdersBL().Update(objOrder);
            Session["Id"] = null;
            details.Visible = false;
            Functions.Alert("Đơn hàng đã được xử lý!!!");
            ShowData();
        }
        protected void linkUnCheckAll_Click(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            foreach (GridViewRow item in gridView.Rows)
            {
                foreach (Control ctrl in item.Cells[1].Controls)
                {
                    if (ctrl.GetType() == typeof(CheckBox))
                    {
                        CheckBox check = (CheckBox)ctrl;
                        check.Checked = false;
                        break;
                    }
                }
            }
        }
        protected void dropIsView_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            ShowData();
        }
        protected void dropSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            ShowData();
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0)
            {
                try
                {
                    foreach (GridViewRow item in gridView.Rows)
                    {
                        int customerId = Convert.ToInt32(item.Cells[3].Text);
                        cmsCustomerDO itemDO = new cmsCustomerDO();
                        cmsCustomerBL itemBL = new cmsCustomerBL();
                        itemDO.Id = customerId;
                        itemDO = itemBL.Select(itemDO);
                        item.Cells[3].Text = itemDO.FullName;
                    }
                }
                catch { }
            }
            //try
            //{
            //    bool isRead = Convert.ToBoolean(e.Row.Cells[6].Text);
            //    if (isRead) e.Row.Cells[6].Text = "Đã đọc";
            //    else
            //    {
            //        e.Row.Cells[6].Text = "Chưa đọc";
            //        e.Row.ForeColor = System.Drawing.Color.Red;
            //    }

            //    e.Row.Cells[5].Text = Convert.ToDateTime(e.Row.Cells[5].Text).ToShortDateString();
            //}
            //catch
            //{

            //}
        }
        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                lbThongBao.Text = "";
                int index = Convert.ToInt32(e.CommandArgument);
                int id = Convert.ToInt32(gridView.Rows[index].Cells[2].Text);
                cmsOrdersDO itemOrderDO = new cmsOrdersDO();
                Session["Id"] = id;
                itemOrderDO.Id = id;
                itemOrderDO = new cmsOrdersBL().Select(itemOrderDO);

                int customerId = itemOrderDO.CustomerId;
                cmsCustomerDO itemCustomerDO = new cmsCustomerDO();
                cmsCustomerBL itemCustomerBL = new cmsCustomerBL();
                itemCustomerDO.Id = customerId;
                itemCustomerDO = itemCustomerBL.Select(itemCustomerDO);
                //Contact obj = contactData.GetById(id);
                ltAddress.Text = itemOrderDO.Address;
                //ltContent.Text = HttpUtility.HtmlDecode(obj.Content);
                //ltDateSend.Text = obj.DateSend.ToString();
                ltEmail.Text = itemCustomerDO.Email;
                ltName.Text = itemCustomerDO.FullName;
                ltPhone.Text = itemCustomerDO.Phone;
                ltNgaydat.Text = itemOrderDO.CreateDate.ToString("dd/MM/yyyy");
                ltNgaygiao.Text = itemOrderDO.DateOfStart.ToString("dd/MM/yyyy");
                //ltTitle.Text = obj.Title;
                details.Visible = true;
                LoadDetail(id);
                //contactData.Read(id);
               
            }
            catch
            {

            }
        }

        void LoadDetail(int OrderID)
        {
            listDetail.DataSource = new cmsOrderDetailsBL().GetByOrderId(OrderID);
            listDetail.DataBind();
        }

        protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
            ShowData();
        }

        public int Total = 0;
        protected void listDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            foreach (GridViewRow item in listDetail.Rows)
            {
                            int Price = Convert.ToInt32(item.Cells[5].Text.Replace(",", "").Replace("VNĐ","").Trim());
                            Total += Price;
            }
        }

        protected void btnBoqua_Click(object sender, EventArgs e)
        {
            details.Visible = false;
            ShowData();
        }

        //protected void listDetail_RowCommand(object sender, GridViewCommandEventArgs e)
        //{
        //    foreach (GridViewRow item in listDetail.Rows)
        //    {
        //        int Price = Convert.ToInt32(item.Cells[5].Text.Replace(",", "").Replace("VNĐ", "").Trim());
        //        Total += Price;
        //    }
        //}
    }
}